/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.scriptprogram;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import net.zarubsys.commons.scripting.Script;
import net.zarubsys.commons.scripting.ScriptingEngine;
import net.zarubsys.commons.scripting.ScriptingEnvironment;
import net.zarubsys.commons.scripting.ScriptingException;
import net.zarubsys.commons.scripting.variables.IVariablePool;
import net.zarubsys.commons.scripting.variables.MapVariablePool;
import net.zarubsys.robotplayground.core.Environment;
import net.zarubsys.robotplayground.core.program.API;
import net.zarubsys.robotplayground.core.program.IProgram;
import net.zarubsys.robotplayground.core.program.IStopListener;

import org.apache.log4j.Logger;

/**
 * ScriptProgram
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class ScriptProgram implements IProgram<AbstractScriptProgramConfiguration> {
	
	private static final Logger LOG = Logger.getLogger(ScriptProgram.class);
	
	public static final String ID = "simpleScriptProgram";

	@Override
	public void run(API api, AbstractScriptProgramConfiguration configuration, Environment env) {
		try (Reader r = configuration.getProgramReader()) {
			Script script = ScriptingEngine.parseScript(r);
			List<IVariablePool> pools = new ArrayList<>();
			if (configuration.getVariablePoolName() != null) {
				IVariablePool pool = new MapVariablePool(configuration.getVariablePoolName(), configuration.getInitState());
				pools.add(pool);
			}
			ScriptingEnvironment scriptingEnvironment = new ScriptingEnvironment(pools, env.getLogger());
			env.registerStopListener(new StopListener(scriptingEnvironment));
			scriptingEnvironment.setMissingFunctionHandler(new APIMissingFunctionHandler(api));
			script.executeScript(scriptingEnvironment);
		} catch (ScriptingException | IOException e) {
			LOG.error("Error while parsing script - " + e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public String getId() {
		return ID;
	}
	
	private static class StopListener implements IStopListener {
		
		private final ScriptingEnvironment scriptingEnvironment;

		public StopListener(ScriptingEnvironment env) {
			this.scriptingEnvironment = env;
		}

		@Override
		public void stop(Object caller, String message) {
			scriptingEnvironment.stop();
		}
		
	}

}

